# -*- coding: UTF-8 -*-
from flask_wtf import FlaskForm
from wtforms import StringField, FloatField, IntegerField, SubmitField, SelectField, RadioField, core
from wtforms.validators import DataRequired, ValidationError


class PrForm(FlaskForm):

    def __init__(self, *args, **kwargs):
        super(PrForm, self).__init__(*args, **kwargs)

    budget = SelectField('budget', validators=[DataRequired()],
                         choices=[('预算A', '预算A'), ('预算B', '预算B'), ('预算C', '预算C')],
                         render_kw={'class': 'form-control'})
    name = StringField(label='商品名称', validators=[DataRequired()],
                       render_kw={'class': 'form-control'},
                       default='')
    price = FloatField(label='价格', validators=[DataRequired()],
                       render_kw={'class': 'form-control'},
                       default=0)
    count = IntegerField(label='数量', validators=[DataRequired()],
                         render_kw={'class': 'form-control'},
                         default=1)

    currency = core.RadioField(
        label="货币", validators=[DataRequired()],
        choices=[('RMB', 'RMB'), ('USD', 'USD'), ('EUR', 'EUR')], default='RMB'
    )
    total = FloatField(label='total(RMB)',validators=[], render_kw={'class': 'form-control', 'readonly': True},default=0)

    submit = SubmitField('提交')
    save = SubmitField('保存')

    def validate_price(self, field):
        if field.data < 0:
            raise ValidationError('price 必须大于0')

    def validate_count(self, field):
        if field.data < 1:
            raise ValidationError('count 必须大于1')
